package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.dal.entitys.*;
import com.cskaoyan.shopping.dal.persistence.ItemCatMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.IShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 *
 * @Description:
 * @Author: 周佳豪
 * @Date: 2022/04/22/21:40
 */
@Service
public class ISoppingServiceImpl implements IShoppingService {

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemCatMapper itemCatMapper;


    @Autowired
    ContentConverter contentConverter;

    @Override
    public List<Panel> getHomepage() {
        List<Panel> panels = panelMapper.selectAll();

        for (Panel panel : panels) {
            int panelId = panel.getId();
            Example example = new Example(PanelContent.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("panelId", panelId);
            List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
            //用converter转换为panels需要的list
            List<PanelContentItem> panelContentItems = contentConverter.panelContents2Item(panelContents);


            for (PanelContentItem panelContentItem : panelContentItems) {
                Long productId = panelContentItem.getProductId();
                Item item = itemMapper.selectByPrimaryKey(productId);
                //判断是否为空
                if (item != null) {
                    panelContentItem.setProductName(item.getTitle());
                    panelContentItem.setSalePrice(item.getPrice());
                    panelContentItem.setSubTitle(item.getSellPoint());
                }
            }
            panel.setPanelContentItems(panelContentItems);

        }
        HomePageResponse homePageResponse = new HomePageResponse();
        Set<PanelDto> panelDtos = contentConverter.list2Set(panels);

        return panels;
    }

    @Override
    public NavListResponse getNavigation() {
        NavListResponse navListResponse = new NavListResponse();
        Example example = new Example(PanelContent.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("panelId", 0);
        try {
            List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
            List<PanelContentDto> panelContentDtos = contentConverter.panelContents2Dto(panelContents);
            navListResponse.setPannelContentDtos(panelContentDtos);
            navListResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            navListResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            navListResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            navListResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
        }
        return navListResponse;
    }

    @Override
    public AllProductCateResponse getAllProductCate() {

        AllProductCateResponse allProductCateResponse = new AllProductCateResponse();
        try {
            List<ItemCat> itemCats = itemCatMapper.selectAll();
            List<ProductCateDto> productCateDtos = contentConverter.itemCats2Dto(itemCats);
            allProductCateResponse.setProductCateDtoList(productCateDtos);
            allProductCateResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            allProductCateResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            allProductCateResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
            allProductCateResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());

        }
        return allProductCateResponse;
    }
}
